﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity;

using BO;
using BrightIdeasSoftware;
using UI.Core;
namespace UI.Security
{
    public partial class SysUserForm :EditFormBase,IMenuForm
    {
        public SysUserForm()
        {
            InitializeComponent();

            MyGrid = grid;
            MyGrid.MultiSelect = false;
            MyGrid.SelectionChanged += grid_SelectionChanged;
            
        }
        SysUser CurObj = null;
        void grid_SelectionChanged(object sender, EventArgs e)
        {
            grid2.ClearObjects();
            var obj = grid.SelectedObject as SysUser;
            if (obj != null)
            {
                
              
                CurObj = obj;
                grid2.Objects = CurObj.ISysUserRoleList;
            }


        }
        
        protected override object OnAdd()
        {
            var obj = new SysUser();
            return obj;

        }
     

        private void SysUserForm_Load(object sender, EventArgs e)
        {
            var cols = new List<OLVColumn>();
            cols.Add(new OLVColumn("角色名", DataBaseObject.Columns.SysUserRole_RoleName));
         
            grid2.Columns.AddRange(cols.ToArray());

            OnSetGridColumns();
            InitGrid();
            OnQuery();
            MyGrid.RowObjectType = typeof(SysUser);
           
        }
        SysUserList mySource;
        protected override void OnQuery()
        {
         
            FetchData();
            DataBind();
        }
        protected override void FetchData()
        {
            mySource = ListFactory<SysUserList>.GetList("");
             
            GridDataSource = mySource;
    
        }
        protected override void DataBind()
        {
            base.DataBind();
        
        }
        void OnSetGridColumns()
        {
            var cols = new List<OLVColumn>();
            cols.Add(new OLVColumn("用户名", DataBaseObject.Columns.SysUser_UserName) { Width = 250 });
            cols.Add(new OLVColumn("密码", DataBaseObject.Columns.SysUser_UserPwd) { Width = 250 });
            cols.Add(new OLVColumn("分管公司", DataBaseObject.Columns.SysUser_CompanyName) { Width = 250 });
            cols.Add(new OLVColumn("管理所有公司", DataBaseObject.Columns.SysUser_AllowAllCompany) 
            { Width = 50, AspectToStringConverter = new AspectToStringConverterDelegate(item => (bool)item == true ? "是" : "否") });
            cols.Add(new OLVColumn("已停用", "IsInactive") { Width = 50
            , AspectToStringConverter = new AspectToStringConverterDelegate(item => (bool)item == true ? "已停用" : "正常")
            });

            MyGrid.Columns.AddRange(cols.ToArray());


        }

        protected override void OnSave()
        {
            try
            {
                foreach (SysUser delObj in RemovedList)
                {
                    if (delObj == null)
                        continue;
                     mySource.Remove(delObj);
                }
                RemovedList.Clear();

                foreach (SysUser obj in MyGrid.Objects)
                {
                    if (obj == null)
                        continue;
                    if (obj.IsChild == false)
                        mySource.Add(obj);

                }

           
                mySource.Save();
                OnQuery();
            }
            catch (ExceptionEx exp)
            {
                ExcptionHandler.ShowExcption(exp);
            }

        }

        private void btnAddObj_Click(object sender, EventArgs e)
        {
            if (CurObj == null)
                return;
            var dform = new DataLookupForm<SysRoleList>();
            if (dform.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                foreach (var item in dform.ReturnObjects)
                {
                    var role = item as SysRole;
                    if (role != null)
                    {
                        if (CurObj.ISysUserRoleList.GetByOID(CurObj.UserName, role.RoleName) == null)
                            CurObj.ISysUserRoleList.Add(new SysUserRole() { RoleName = role.RoleName, UserName = CurObj.UserName });
                    }
                }
                grid2.Objects = CurObj.ISysUserRoleList;
            }
        }

        private void btnRemoveObj_Click(object sender, EventArgs e)
        {
            CurObj.ISysUserRoleList.Remove(grid2.SelectedObject as SysUserRole);
            grid2.Objects = CurObj.ISysUserRoleList;
        }
    }
}
